package com.ben.xinyue.seckill.controller;

import com.ben.basic.exception.BaseResponse;
import com.ben.basic.exception.DefaultSuccessEnum;
import com.ben.xinyue.seckill.domain.entity.MiaoshaUser;
import com.ben.xinyue.seckill.domain.vo.GoodsDetailVo;
import com.ben.xinyue.seckill.domain.vo.GoodsVo;
import com.ben.xinyue.seckill.service.GoodsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: GoodsController
 * @Description: 商品数据
 * @author: xinyue
 * @date: 2020/9/8 9:35 下午
 * @version: 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("goods")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GoodsController {

    private final GoodsService goodsService;

    @RequestMapping(value = "goods/detail/{goodsId}")
    @ResponseBody
    public BaseResponse goodsList(MiaoshaUser user, @PathVariable("goodsId")long goodsId){
        GoodsVo goods = goodsService.queryGoodsVoByGoodsId(goodsId);
        long startAt = goods.getStartDate().getTime();
        long endAt = goods.getEndDate().getTime();
        long now = System.currentTimeMillis();
        int remainSeconds =0;
        if(now < startAt){
            remainSeconds=(int)(startAt- now)/1000;
        }else if (now >endAt){
            remainSeconds=-1;
        }
        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        goodsDetailVo.setGoods(goods).setUser(user).setRemainSeconds(remainSeconds);
        return BaseResponse.business(DefaultSuccessEnum.SUCCESS, goodsDetailVo);
    }

}
